/** @file    WidgetUpdateBar.cpp
 *  @author  Alessandro Polo
 *  @version $Id: WidgetUpdateBar.cpp 2461 2010-05-13 00:07:57Z alex $
 *  @brief
 * File containing methods for the wosh::gui::WidgetUpdateBar class.
 * The header for this class can be found in WidgetUpdateBar.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "WidgetUpdateBar.h"

 #include "ui_WidgetUpdateBar.h"
 #include <QDateTime>


using namespace std;
namespace wosh {
 namespace gui {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WidgetUpdateBar::WidgetUpdateBar(QWidget *parent) : QWidget(parent)
 {
	this->m_ui = new Ui::WidgetUpdateBar();
	this->m_ui->setupUi(this);
	this->m_ui->lblMessage->
	connect(this->m_ui->btnUpdate, SIGNAL(clicked()), this, SIGNAL(updateClicked()));

 }

WidgetUpdateBar::~WidgetUpdateBar()
 {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void WidgetUpdateBar::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        this->m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void WidgetUpdateBar::sayReady() {
	showMessage( tr("Updated contents."), true );
 }

void WidgetUpdateBar::sayRetrieving() {
	this->requestTs = QDateTime::currentDateTime();
	showMessage( tr("Retrieving contents.."), false );
 }

void WidgetUpdateBar::showMessage( const QString& message, bool show_delay ) {
	this->m_ui->lblMessage->setText( QString("<font color='gray'>%1</font>").arg(message) );
	QDateTime dtnow = QDateTime::currentDateTime();
	this->m_ui->lblTimeStamp->setText(dtnow.toString("hh:mm:ss"));
	this->m_ui->lblDelay->setVisible(show_delay);
	if ( !show_delay )
		return;
	if ( !this->requestTs.isValid() ) {
		this->m_ui->lblDelay->setText( tr(" [asynch]") );
		return;
	 }
	qint64 tts = this->requestTs.time().secsTo(dtnow.time());
	qint64 ttms = this->requestTs.time().msecsTo(dtnow.time());
	if ( tts == 0 && ttms == 0 )
		this->m_ui->lblDelay->setVisible(false);
	else
		this->m_ui->lblDelay->setText(QString(" [%1:%2]").arg(tts).arg(ttms%1000));
	this->requestTs = QDateTime(); // null
 }

void WidgetUpdateBar::showError( const QString& message ) {
	this->m_ui->lblMessage->setText(message);
	this->m_ui->lblTimeStamp->setText(QDateTime::currentDateTime().toString("hh:mm:ss.zzz"));
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

QToolButton* WidgetUpdateBar::getUpdateButton() {
	return this->m_ui->btnUpdate;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
